package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.mapper.DtsCouponUserMapper;
import com.qf.pojo.DtsBrand;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class CouponServiceImpl implements CouponService{
    @Autowired
    private DtsCouponMapper dtsCouponMapper;
    @Autowired
    private DtsCouponUserMapper dtsCouponUserMapper;


    @Override
    public IPage<DtsCoupon> selectPage(int page, int limit, String sort, String order, Integer type, String name, Integer status) {
        QueryWrapper<DtsCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name),"name",name);
        queryWrapper.like(!StringUtils.isEmpty(type),"type",type);
        queryWrapper.eq(!StringUtils.isEmpty(status),"status",status);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);

        return dtsCouponMapper.selectPage(new Page<DtsCoupon>(page,limit),queryWrapper);
    }

    @Override
    public DtsCoupon selectOn(Integer id) {
        QueryWrapper<DtsCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return dtsCouponMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<DtsCouponUser> selectlistuser(int page, int limit, String sort, String order, Integer couponId, Integer userId, Integer status) {
        QueryWrapper<DtsCouponUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(couponId),"coupon_id",couponId);
        queryWrapper.eq(!StringUtils.isEmpty(userId),"user_id",userId);
        queryWrapper.eq(!StringUtils.isEmpty(status),"status",status);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);

        return dtsCouponUserMapper.selectPage(new Page<DtsCouponUser>(page,limit),queryWrapper);
    }

    @Override
    public void creatCoupon(DtsCoupon dtsCoupon) {
        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponMapper.insert(dtsCoupon);
    }

    @Override
    public void updateCoupon(DtsCoupon dtsCoupon) {
        dtsCoupon.setAddTime(new Date());
        dtsCoupon.setUpdateTime(new Date());
        dtsCouponMapper.updateById(dtsCoupon);
    }

    @Override
    public void deleteCoupon(DtsCoupon dtsCoupon) {
        dtsCouponMapper.deleteById(dtsCoupon);
    }
}
